// Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview This module provides a way to measure response times via HTTP.
 *
 * @author gavaletz@google.com (Eric Gavaletz)
 */


goog.provide('resptime');


/**
 * The limit (upper) for the number of files to be loaded.
 *
 * @type {number}
 */
resptime.maxPings = 100;


/**
 * The count for the number of files loaded.
 *
 * @type {number}
 */
resptime.pingCount = 0;


/**
 * The timestamp for when the file was requested.
 *
 * @type {number}
 */
resptime.startTime = 0;


/**
 * Storage for the file response times.
 *
 * @type {Array.<number>}
 */
resptime.delay = [];


/**
 * The maximum amount of time that this test should be allowed to run.
 *
 * @type {number}
 */
resptime.timeLimit = 1000.0;


/**
 * The limit (lower) for the number of files to be loaded.
 *
 * @type {number}
 */
resptime.minPings = 10;


/**
 * Indicates that a test is in progress.
 *
 * @type {boolean}
 */
resptime.testInProgress = false;


/**
 * Function to receive the data.
 *
 * @type {function(Array.<number>)}
 */
resptime.callback;


/**
 * Function to receive status updates.
 *
 * @type {function(number)}
 */
resptime.progress;


/**
 * Sets up the netscore state to receive updates and loads the first image.
 *
 * @param {string} server The server to use.
 * @param {string} resource The complete resource path.
 * @param {number} maxPings The maximum number of files to load.
 * @param {number} minPings The minimum number of files to load.
 * @param {number} timeLimit The maximum amount of time to take (seconds).
 * @param {function(Array.<number>)} callback to receive the data.
 * @param {function(number)} progressCallback to receive status updates.
 */
resptime.setup = function(server, resource, maxPings, minPings, timeLimit,
    callback, progressCallback) {
  if (resptime.testInProgress) {
    datastore.logError('resptimeError', 'testInProgress is true');
    return;
  }
  else {
    resptime.testInProgress = true;
  }
  resptime.pingCount = 0;
  resptime.delay = [];
  resptime.imgBase = 'http://' + server + resource + '?p=' +
      new Date().getTime() + '.';
  resptime.frame = goog.dom.createDom('img', {'src': '/images/blank.gif',
    'alt': 'this should never be seen',
    'style': 'width: 0px; height: 0px; visibility:hidden;'});
  resptime.maxPings = maxPings;
  resptime.minPings = minPings;
  resptime.timeLimit = timeLimit;
  resptime.callback = callback;
  resptime.progress = progressCallback;
};


/**
 * Sets the test in motion.
 */
resptime.start = function() {
  resptime.beginTime = new Date().getTime();
  resptime.startTime = resptime.beginTime;
  resptime.pingCount = 0;
  resptime.frame.onload = resptime.done;
  resptime.frame.src = resptime.imgBase + resptime.pingCount;
};


/**
 * Performs all the successive image loads.
 */
resptime.done = function() {
  var endTime = new Date().getTime();
  resptime.delay[resptime.pingCount] = endTime - resptime.startTime;
  var duration = endTime - resptime.beginTime;
  var p1 = duration / resptime.timeLimit;
  var p2 = resptime.pingCount / resptime.maxPings;
  resptime.progress(Math.max(p1, p2) * 100);
  var a = resptime.pingCount < resptime.maxPings;
  var b = resptime.pingCount < resptime.minPings;
  var c = duration < resptime.timeLimit;
  if ((c && a) || b) {
    resptime.pingCount += 1;
    resptime.startTime = new Date().getTime();
    resptime.frame.src = resptime.imgBase + resptime.pingCount;
  }
  else
  {
    resptime.frame.onload = null;
    resptime.finish();
  }
};


/**
 * Passes back a copy of the data.
 */
resptime.finish = function() {
  resptime.testInProgress = false;
  resptime.callback(resptime.delay);
};
